package educrypt.io;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

import org.apache.poi.POITextExtractor;
import org.apache.poi.extractor.ExtractorFactory;

import com.ibm.icu.text.CharsetDetector;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;

import educrypt.exceptions.EduCryptException;
import educrypt.i18n.Messages;

public class Input {

	private static int BUFFER_SIZE = 102400;

	public static String readTextFromFile(String path) throws EduCryptException {
		String text = "";

		String mimeType = FileUtil.getMimeType(path);
		if (mimeType.equals(FileUtil.MIMETYPE_PLAINTEXT)) {
			text = readTextFile(path);
		}
		if (mimeType.equals(FileUtil.MIMETYPE_PDF)) {
			text = readPdfFile(path);
		}
		if (mimeType.equals(FileUtil.MIMETYPE_DOC)) {
			text = readAnyDocFile(path);
		}
		if (mimeType.equals(FileUtil.MIMETYPE_DOCX)) {
			text = readAnyDocFile(path);
		}
		return text;
	}

	public static String readTextFile(String path) throws EduCryptException {
		String text = "";
		try {
			BufferedInputStream in = new BufferedInputStream(
					new FileInputStream(path));
			text = readTextFile(in);
			in.close();
		} catch (FileNotFoundException e) {
			throw new EduCryptException(Messages.getString("errFileNotFound"));
		} catch (IOException e) {
			throw new EduCryptException(Messages.getString("errReadingFile"));
		}
		return text;
	}

	public static String readTextFile(InputStream in) throws EduCryptException {
		String text = "";
		try {
			CharsetDetector charsetDetector = new CharsetDetector();
			Reader reader = charsetDetector.getReader(in, null);

			text = readBuffered(reader);
		} catch (IOException e) {
			throw new EduCryptException(Messages.getString("errReadingFile"));
		}
		return text;
	}

	private static String readBuffered(Reader reader) throws IOException {
		String text = "";
		BufferedReader br = new BufferedReader(reader);

		char[] buffer = new char[BUFFER_SIZE];
		int l;
		while ((l = br.read(buffer)) != -1) {
			text += new String(buffer, 0, l);
		}
		return text;
	}

	public static String readAnyDocFile(String path) throws EduCryptException {
		String inputData = "";
		try {
			POITextExtractor poitex = ExtractorFactory
					.createExtractor(new FileInputStream(new File(path)));
			inputData = poitex.getText();
		} catch (FileNotFoundException e) {
			throw new EduCryptException(Messages.getString("errFileNotFound"));
		} catch (Exception e) {
			throw new EduCryptException(Messages.getString("errReadingFile"));
		}
		return inputData;
	}

	public static String readPdfFile(String path) throws EduCryptException {
		String inputData = "";
		try {
			PdfReader reader = new PdfReader(path);
			int numberOfPages = reader.getNumberOfPages();
			for (int i = 1; i <= numberOfPages; i++) {
				inputData += PdfTextExtractor.getTextFromPage(reader, i);
			}
		} catch (IOException e) {
			throw new EduCryptException(Messages.getString("errReadingFile"));
		}
		return inputData;
	}
}
